package com.example.mykotlinapp

import android.view.View
import android.widget.Button
import java.io.File


/**
 *
 *
 * @author zhudeshuai
 * @since 2019/7/3
 */
/*data class Person(val name: String)

//object
///对象声明，创建单例
object Payroll {
    val allEmployees = arrayListOf<Person>()
    fun caculateSalary() {
        for (person in allEmployees) {
            TODO()
        }
    }
}

//匿名内部类，实现比较器接口
object CaseInsensitiveFileComparator : Comparator<File> {
    override fun compare(o1: File, o2: File): Int {
        return o1.path.compareTo(o2.path, true)
    }

}

private fun test60() {
    Payroll.allEmployees.add(Person("Alice"))
    println(CaseInsensitiveFileComparator.compare(File("/User"), File("/User")))
}*/

//伴生对象
class A {
    companion object {
        fun bar() {
            println("Companion object called")
        }
    }
}

///工厂方法
class User private constructor(val nickName: String) {
    companion object {
        fun newSubscribingUser(email: String) = User(email.substringBefore('@'))
        fun newFacebookUser(accountId: Int) = User(accountId.toString())
    }
}

//伴生对象实现接口，可以把该伴生对象所在类名当做该接口的实现使用

//对象表达式或者匿名内部类使用
val listener = object : View.OnClickListener {
    override fun onClick(v: View?) {

    }

}

//lambda编程来了

///事例1 代替匿名内部类
private fun test51() {
    val button = Button(null)
    button.setOnClickListener {
        TODO("执行点击操作")
    }
}

///事例2 与集合一起使用
data class Person(val name: String, val age: Int)

private fun test52() {
    val list = listOf(Person("Tom", 18), Person("Jack", 14))
    println(list.maxBy { it.age })
    println(list.maxBy(Person::age))
}

///lambda语法
private fun test53() {
    val sum = { x: Int, y: Int -> x + y }
    println(sum(1, 2))

}

private fun printMessageWithPrefix(message: Collection<String>, prefix: String) {
    message.forEach {
        println("$prefix$it")
    }
}


private fun main() {
    printMessageWithPrefix(arrayListOf("A","B","C"),"_")
}